<template>
  <div class="system-role-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
      <el-form ref="permissionDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
        <el-row :gutter="35">

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="权限分类" prop="permissionType">
              <el-select v-model="state.ruleForm.permissionType" placeholder="选择分类" style="width: 100%">
                <el-option
                    v-for="item in state.permissionTypes"
                    :key="item.type"
                    :label="item.title"
                    :value="item.type"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="权限名称" prop="permissionName">
              <el-input v-model="state.ruleForm.permissionName" placeholder="请输入权限名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="权限标识" prop="permissionCode">
              <el-input v-model="state.ruleForm.permissionCode" placeholder="请输入权限标识" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="角色描述">
              <el-input v-model="state.ruleForm.describe" type="textarea" placeholder="请输入权限描述"
                        maxlength="150"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit(permissionDialogFormRef)" size="default">{{
              state.dialog.submitTxt
            }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import {reactive, ref, nextTick} from 'vue';
import {permissionApi} from '/@/api/sys/permission';

import {dictApi} from '/@/api/sys/dict'
import type {FormInstance, FormRules} from 'element-plus';
import msg from '/@/utils/messageUtils';
import messageUtils from "/@/utils/messageUtils";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const permissionDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: '0',
    permissionName: '',
    permissionCode: '',
    describe: '',
    permissionType: '',
  },
  permissionTypes: [],
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
});

const rules = reactive({
  permissionName: [{required: true, message: '请输入权限名称', trigger: 'blur'}],
  permissionCode: [{required: true, message: '请输入权限标识', trigger: 'blur'}],
  permissionType: [{required: true, message: '请选择权限分类', trigger: 'blur'}],
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
  onLoadDict()
  if (type === 'edit') {
    state.dialog.title = '修改权限';
    state.dialog.submitTxt = '修 改';
    state.dialog.type = 'edit';
    state.ruleForm.id = row.id;
    viewPermission(row.id);
  } else {
    state.dialog.title = '新增权限';
    state.dialog.submitTxt = '新 增';
    state.dialog.type = type;
    nextTick(() => {
      permissionDialogFormRef.value.resetFields();
    });
  }
  state.dialog.isShowDialog = true;
};


const onLoadDict = async () => {
  try {
    const {data} = await dictApi().queryByCategory("api_permission")
    state.permissionTypes = data
    console.log(data)
  } catch (err) {
    messageUtils().error("系统异常稍后重试");
  }
}
const viewPermission = async (permissionId: string) => {
  try {
    const {data} = await permissionApi().viewPermission(permissionId);
    state.ruleForm = data;
  } catch (error) {
    msg().error('获取权限详细信息失败,请重试');
  }
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid) => {
    if (valid) {
      try {
        if (state.dialog.type === 'add') {
          await permissionApi().createPermission(state.ruleForm);
        } else {
          await permissionApi().updatePermission(state.ruleForm);
        }
        msg().success('操作成功');
        closeDialog();
        emit('refresh');
      } catch (error) {
        msg().error(error);
      }
    }
  });

  // if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
  .menu-data-tree {
    width: 100%;
    border: 1px solid var(--el-border-color);
    border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
    padding: 5px;
  }
}
</style>
